package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.RoleTypeConstants;
import com.campus.common.core.Pager;
import com.campus.mapper.ManagerTeacherMapper;
import com.campus.mapper.StudentMapper;
import com.campus.mapper.UserRoleMapper;
import com.campus.mapping.ManagerStudentMapping;
import com.campus.mapping.ManagerTeacherMapping;
import com.campus.model.TManagerClass;
import com.campus.model.TManagerStudent;
import com.campus.model.TManagerTeacher;
import com.campus.model.TSysUserRole;
import com.campus.model.dto.StudentSelectCondition;
import com.campus.model.dto.TManagerStudentCreateDto;
import com.campus.model.dto.TManagerTeacherCreateDto;
import com.campus.model.vo.ClassPageVo;
import com.campus.model.vo.StudentSelectVo;
import com.campus.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, TManagerStudent>
        implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Integer createStudent(TManagerStudentCreateDto createDto) {

        TManagerStudent managerStudent = ManagerStudentMapping.INSTANCE.CreatetoStudent(createDto);

        TSysUserRole userRole = new TSysUserRole();
        userRole.setUserId(createDto.getLoginId());
        userRole.setRoleId(RoleTypeConstants.STUDENT);
        userRoleMapper.insert(userRole);

        return studentMapper.insert(managerStudent);
    }

    @Override
    public Page<StudentSelectVo> list(Pager pager, StudentSelectCondition conditionDto) {

        QueryWrapper<TManagerStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(conditionDto.getStudentName()),"t1.stu_name",conditionDto.getStudentName());
        queryWrapper.like(StringUtils.isNotBlank(conditionDto.getClassName()),"t3.`name`",conditionDto.getClassName());
        queryWrapper.like(StringUtils.isNotBlank(conditionDto.getLoginName()),"t6.login_name",conditionDto.getLoginName());
        queryWrapper.like(StringUtils.isNotBlank(conditionDto.getTeacherName()),"t4.teacher_name",conditionDto.getTeacherName());
        queryWrapper.like(StringUtils.isNotBlank(conditionDto.getActivityName()),"t5.name",conditionDto.getActivityName());

        Page p = new Page<>(pager.getPageNum(), pager.getPageSize());
        return studentMapper.page(p, queryWrapper);
    }

    @Override
    public List<TManagerStudent> listByStatus(String Status) {
        QueryWrapper<TManagerStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",Status);
        return studentMapper.selectList(queryWrapper);
    }

    @Override
    public Integer checkStudent(Long id) {
        TManagerStudent tManagerStudent = new TManagerStudent();
        tManagerStudent.setId(id);
        tManagerStudent.setStatus("1");
        return studentMapper.updateById(tManagerStudent);

    }

    @Override
    public Integer checkStudentBatch(List<Long> ids) {
        UpdateWrapper<TManagerStudent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",ids);
        updateWrapper.set("status","1");
        return studentMapper.update(null,updateWrapper);
    }
}
